package com.extweb4j.web.controller;

import com.extweb4j.core.anno.AuthAnno;
import com.extweb4j.core.anno.Log;
import com.extweb4j.core.bean.PageInfo;
import com.extweb4j.core.controller.ExtController;
import com.extweb4j.core.enums.RowStatus;
import com.extweb4j.core.kit.ExtKit;
import com.extweb4j.core.kit.Pinyin4jKit;
import com.extweb4j.core.model.ExtTender;
import com.extweb4j.core.model.ExtTenderSection;
import com.extweb4j.core.model.ExtUser;
import com.extweb4j.core.model.ExtUserRole;
import com.extweb4j.web.bean.News;
import com.extweb4j.web.bean.Tender;
import com.extweb4j.web.bean.TenderPeople;
import com.extweb4j.web.utils.DateUtils;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ZhaoWeinan
 * @date 2018/12/16
 * @description
 */
public class TenderController extends ExtController {


    /**
     * 列表
     */
    @AuthAnno
    public void list(){
   /*     int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");

        PageInfo<Tender> pageInfo = new PageInfo<>();

        Tender tender1 = new Tender();
        tender1.setId(1L);
        tender1.setNum("DXGSZBGG-2018-085-02号");
        tender1.setTitle("蒙东协合公司供热站站内设备综合技术改造招标二次公告");
        tender1.setType("变更公告");
        tender1.setPeople("国家电投集团内蒙古能源有限公司");
        tender1.setCreate_time(new Date());
        tender1.setStart_time(new Date());
        tender1.setEnd_time(new Date());
        tender1.setState("已审核");
        tender1.setUser("采购部管理员");


        Tender tender2 = new Tender();
        tender2.setId(2L);
        tender2.setNum("DXGSZBGG-2018-084");
        tender2.setTitle("矿建公司2018年报废车辆 公开竞价销售二次公告");
        tender2.setType("竞价公告");
        tender2.setPeople("内蒙古霍林河露天煤业股份有限公司煤矿建设管理分公司");
        tender2.setCreate_time(new Date());
        tender2.setStart_time(new Date());
        tender2.setEnd_time(new Date());
        tender2.setState("新建");
        tender2.setUser("采购部管理员");


        List<Tender> list = new ArrayList<>();
        list.add(tender1);
        list.add(tender2);

        pageInfo.setFirstPage(true);
        pageInfo.setLastPage(true);
        pageInfo.setPageNumber(1);
        pageInfo.setPageSize(20);
        pageInfo.setTotalPage(1);
        pageInfo.setTotalRow(2);

        pageInfo.setList(list);
        renderJson(pageInfo);*/

        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");

        Page<ExtTender> pageData = ExtTender.dao.pageUserBy(page,limit,keywords);
        renderJson(pageData);
    }


    @AuthAnno
    public void section(){

        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");
        int tenderId = getParaToInt("tenderId");

        Page<ExtTenderSection> pageData = ExtTenderSection.dao.pageSectionBy(page,limit,keywords,tenderId);
        renderJson(pageData);
    }

    /**
     * 新增
     */
    @Log("新建招标")
    @AuthAnno
    @Before(Tx.class)
    public void add(){
        //ExtUser user = getExtModel(ExtUser.class);
        ExtTender tender = new ExtTender();
        Map<String, String[]> parasMap = getRequest().getParameterMap();
        if (parasMap.get("title") != null && parasMap.get("title").length > 0){
            tender.set("title",parasMap.get("title")[0]);
        }

        if (parasMap.get("tenderno") != null && parasMap.get("tenderno").length > 0){
            tender.set("tenderno",parasMap.get("tenderno")[0]);
        }

        if (parasMap.get("tenderarea") != null && parasMap.get("tenderarea").length > 0){
            tender.set("tenderarea",parasMap.get("tenderarea")[0]);
        }

        if (parasMap.get("noticetype") != null && parasMap.get("noticetype").length > 0){
            tender.set("noticetype",parasMap.get("noticetype")[0]);
        }

        if (parasMap.get("source") != null && parasMap.get("source").length > 0){
            tender.set("source",parasMap.get("source")[0]);
        }

        if (parasMap.get("tenderman") != null && parasMap.get("tenderman").length > 0){
            tender.set("tenderman",parasMap.get("tenderman")[0]);
        }

        String[] section = parasMap.get("section");

        if (section != null && section.length > 0){
            tender.set("section", StringUtils.join(section,","));
        }

        String[] purchasetype = parasMap.get("purchasetype");

        if (purchasetype != null && purchasetype.length > 0){
            tender.set("purchasetype", StringUtils.join(purchasetype,","));
        }

        if(parasMap.get("opentime") != null && parasMap.get("opentime").length > 0){
            try {
                tender.set("opentime", new Timestamp(DateUtils.getDate(parasMap.get("opentime")[0]).getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(parasMap.get("endtime") != null && parasMap.get("endtime").length > 0){
            try {
                tender.set("endtime", new Timestamp(DateUtils.getDate(parasMap.get("endtime")[0]).getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (parasMap.get("noticecontent") != null && parasMap.get("noticecontent").length > 0){
            tender.set("noticecontent",parasMap.get("noticecontent")[0]);
        }


        tender.set("createtime",new Timestamp(System.currentTimeMillis()));
        tender.save();
        success();
    }

    /**
     * 根据Id获取一条记录，编辑
     */
    public void get(){
        ExtTender tender = ExtTender.dao.findById(getPara("id"));

        if (tender != null){
            tender.get("section");
            String[] section = tender.get("section").toString().split(",");
            tender.set("section",section);
            String[] purchasetype = tender.get("purchasetype").toString().split(",");
            tender.set("purchasetype",purchasetype);
        }

        renderJson(new Record().set("success",true).set("data",tender));
    }

    /**
     * 编辑
     */
    @Log("编辑招标")
    @AuthAnno
    @Before(Tx.class)
    public void edit(){
        ExtTender tender = new ExtTender();
        Map<String, String[]> parasMap = getRequest().getParameterMap();
        if (parasMap.get("title") != null && parasMap.get("title").length > 0){
            tender.set("title",parasMap.get("title")[0]);
        }

        if (parasMap.get("id") != null && parasMap.get("id").length > 0){
            tender.set("id",parasMap.get("id")[0]);
        }

        if (parasMap.get("tenderno") != null && parasMap.get("tenderno").length > 0){
            tender.set("tenderno",parasMap.get("tenderno")[0]);
        }

        if (parasMap.get("tenderarea") != null && parasMap.get("tenderarea").length > 0){
            tender.set("tenderarea",parasMap.get("tenderarea")[0]);
        }

        if (parasMap.get("noticetype") != null && parasMap.get("noticetype").length > 0){
            tender.set("noticetype",parasMap.get("noticetype")[0]);
        }

        if (parasMap.get("source") != null && parasMap.get("source").length > 0){
            tender.set("source",parasMap.get("source")[0]);
        }

        if (parasMap.get("tenderman") != null && parasMap.get("tenderman").length > 0){
            tender.set("tenderman",parasMap.get("tenderman")[0]);
        }

        String[] section = parasMap.get("section");

        if (section != null && section.length > 0){
            tender.set("section", StringUtils.join(section,","));
        }

        String[] purchasetype = parasMap.get("purchasetype");

        if (purchasetype != null && purchasetype.length > 0){
            tender.set("purchasetype", StringUtils.join(purchasetype,","));
        }

        if(parasMap.get("opentime") != null && parasMap.get("opentime").length > 0){
            try {
                tender.set("opentime", new Timestamp(DateUtils.getDate(parasMap.get("opentime")[0]).getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(parasMap.get("endtime") != null && parasMap.get("endtime").length > 0){
            try {
                tender.set("endtime", new Timestamp(DateUtils.getDate(parasMap.get("endtime")[0]).getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (parasMap.get("noticecontent") != null && parasMap.get("noticecontent").length > 0){
            tender.set("noticecontent",parasMap.get("noticecontent")[0]);
        }

        tender.update();

        success();
    }


    @Log("编辑招标")
    @AuthAnno
    @Before(Tx.class)
    public void exam(){
        ExtTender tender = new ExtTender();
        Map<String, String[]> parasMap = getRequest().getParameterMap();


        if (parasMap.get("id") != null && parasMap.get("id").length > 0){
            tender.set("id",parasMap.get("id")[0]);
        }

        tender.set("ischecked",true);
        tender.update();

        success();
    }


    /**
     * 列表
     */
    @AuthAnno
    public void list1(){
        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");

        PageInfo<TenderPeople> pageInfo = new PageInfo<>();

        TenderPeople tender1 = new TenderPeople();
        tender1.setId(1L);
        tender1.setName("标段1");
        tender1.setFile_name("关于标段1投标文件");
        tender1.setCampany("国家电投集团内蒙古能源有限公司");
        tender1.setCash("10000");
        tender1.setCast("5245");

        TenderPeople tender2 = new TenderPeople();
        tender2.setId(2L);
        tender2.setName("标段2");
        tender2.setFile_name("关于标段2投标文件");
        tender2.setCampany("国家电投集团内蒙古能源有限公司");
        tender2.setCash("10000");
        tender2.setCast("6954");


        List<TenderPeople> list = new ArrayList<>();
        list.add(tender1);
        list.add(tender2);

        pageInfo.setFirstPage(true);
        pageInfo.setLastPage(true);
        pageInfo.setPageNumber(1);
        pageInfo.setPageSize(20);
        pageInfo.setTotalPage(1);
        pageInfo.setTotalRow(2);

        pageInfo.setList(list);
        renderJson(pageInfo);
    }
}
